#ifndef _ONLINE_USER_
#define _ONLINE_USER_

#include"util.hpp"

class Online_Manager
{
public:
    // 进入游戏大厅
    void enter_game_hall(uint64_t uid,const wsserver_t::connection_ptr &conn)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _game_hall.insert(std::make_pair(uid,conn));
    }

    // 退出游戏大厅
    void exit_game_hall(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _game_hall.erase(uid);
    }

    // 进入游戏房间
    void enter_game_room(uint64_t uid,const wsserver_t::connection_ptr &conn)
    {
        // DBG_LOG("enter_game_room begin");
        std::unique_lock<std::mutex> lock(_mutex);
        _game_room.insert(std::make_pair(uid,conn));
    }

    // 退出游戏房间
    void exit_game_room(uint64_t uid)
    {
        // DBG_LOG("exit_game_room begin");
        std::unique_lock<std::mutex> lock(_mutex);
        _game_room.erase(uid);
        // DBG_LOG("exit_game_room tail");
    }

    // 判断是否在游戏大厅
    bool in_game_hall(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        auto it=_game_hall.find(uid);
        if(it==_game_hall.end())
        {
            return false;
        }
        return true;
    }

    // 判断是否在游戏房间
    bool in_game_room(uint64_t uid)
    {
        // DBG_LOG("in_game_room begin");
        std::unique_lock<std::mutex> lock(_mutex);
        auto it=_game_room.find(uid);
        if(it==_game_room.end())
        {
            return false;
        }
        return true;
    }

    // 获取大厅中指定用户的socket连接
    bool get_conn_game_hall(uint64_t uid,wsserver_t::connection_ptr &conn)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        auto it=_game_hall.find(uid);
        if(it==_game_hall.end())
        {
            return false;
        }
        conn=it->second;
        return true;
    }

    // 获取房间中指定用户的socket连接
    bool get_conn_game_room(uint64_t uid,wsserver_t::connection_ptr &conn)
    {
        // DBG_LOG("get_conn_game_room begin");
        std::unique_lock<std::mutex> lock(_mutex);
        auto it=_game_room.find(uid);
        if(it==_game_room.end())
        {
            return false;
        }
        conn=it->second;
        return true;
    }    
private:
    std::unordered_map<uint64_t,wsserver_t::connection_ptr>_game_hall;  //管理游戏大厅用户
    std::unordered_map<uint64_t,wsserver_t::connection_ptr>_game_room;  //管理游戏房间用户
    std::mutex _mutex;
};

#endif